package com.aiyige.utils.widget.articleview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.aiyige.R;
import com.aiyige.utils.widget.articleview.adapter.ArticlePreviewAdapter;
import com.aiyige.utils.widget.articleview.model.ArticleNode;

import java.util.List;

/**
 * Created by zhuyakun on 2018/3/13.
 */

public class ArticlePreview extends FrameLayout {
    RecyclerView articlePreviewRv;
    ArticlePreviewAdapter articlePreviewAdapter;

    public ArticlePreview(@NonNull Context context) {
        super(context);
        init();
    }

    public ArticlePreview(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ArticlePreview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.aritcle_preview, this, true);
        articlePreviewRv = (RecyclerView) findViewById(R.id.articlePreviewRv);
        articlePreviewRv.setLayoutManager(new LinearLayoutManager(getContext()));
        articlePreviewAdapter = new ArticlePreviewAdapter();
        articlePreviewAdapter.bindToRecyclerView(articlePreviewRv);
    }

    public View addHeader(int headLayoutResId) {
        View header = LayoutInflater.from(getContext()).inflate(headLayoutResId, (ViewGroup) articlePreviewRv.getParent(), false);
        articlePreviewAdapter.addHeaderView(header);
        return header;
    }

    public View addFooter(int footerLayoutResId) {
        View footer = LayoutInflater.from(getContext()).inflate(footerLayoutResId, (ViewGroup) articlePreviewRv.getParent(), false);
        articlePreviewAdapter.addFooterView(footer);
        return footer;
    }

    public void setData(List<ArticleNode> data) {
        articlePreviewAdapter.setNewData(data);
    }
}
